<template>
  <div>
    <el-menu
        :default-active="$route.path"
        style="height: calc(100vh - 60px);"
        class="el-menu-vertical-demo"
        @open="handleOpen"
        @close="handleClose"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b">
      <div v-for="item in menus" :key="item.id">
        <div v-if="item.path">
          <el-menu-item :index="item.path" @click="clickMenu(item)">
            <i :class="item.icon"></i>
            <span slot="title">{{ item.name }}</span>
          </el-menu-item>
        </div>
        <div v-else>
          <el-submenu :index="item.id + ''">
            <template slot="title">
              <i :class="item.icon"></i>
              <span slot="title">{{ item.name }}</span>
            </template>
            <div v-for="subItem in item.children" :key="subItem.id">
              <el-menu-item :index="subItem.path" @click="clickMenu(subItem)">
                <i :class="subItem.icon"></i>
                <span slot="title">{{ subItem.name }}</span>
              </el-menu-item>
            </div>
          </el-submenu>
        </div>
      </div>
    </el-menu>
  </div>
</template>

<script>
  export default {
    name: "Sidebar",
    data(){
      return {
        menus:[{
          id: '1',
          path:'/home',
          name:'首页',
          icon:'el-icon-s-home'
        },{
          id: '2',
          path:'',
          name:'项目管理',
          icon:'el-icon-folder',
          children:[{
            id: '2-1',
            path:'/project',
            name:'我的项目',
            icon:'el-icon-magic-stick'
          },{
            id: '2-2',
            path:'/create',
            name:'新建项目',
            icon:'el-icon-document-add'
          },{
            id: '2-3',
            path:'/remember',
            name:'待办事项',
            icon:'el-icon-notebook-2'
          },]
        },{
          id: '3',
          path:'/search',
          name:'项目查询',
          icon:'el-icon-search'
        },{
          id: '4',
          path:'/merits',
          name:'绩效管理',
          icon:'el-icon-s-marketing'
        },{
          id: '5',
          path:'/user',
          name:'人员管理',
          icon:'el-icon-user-solid'
        }]
      }
    },
    methods:{
      handleOpen(key, keyPath) {
        console.log(key, keyPath);
      },
      handleClose(key, keyPath) {
        console.log(key, keyPath);
      },
      clickMenu (item) {
        this.$router.push({path : item.path})
        //存储当前菜单,解决刷新后state状态丢失问题
        // localStorage.setItem("name",item.name)

        this.$store.commit("selectMenu", item)
      }
    }
  }
</script>

<style scoped>
  /*去除白边*/
  .el-menu{
    border: 0 !important;
  }
</style>
